/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVA-Data-Structures-and-Algorithms PROJECT
 * All Rights Reserved.
 */

package 节点;

/**
 * @Description
 * @Author 俊昭
 * @Date 2022/5/4
 */
public class Node {
    public Node left;
    public Node right;
    private int No;
    private String name;

    public Node(int no, String name) {
        No = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "排名：" + No + ", 姓名：" + name;
    }

    /**
     * @description 前序遍历
     * 先输出父节点，再遍历左子树和右子树
     * @author 俊昭
     * @date 2022/5/4
     */
    public void preTraversal() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preTraversal();
        }
        if (this.right != null) {
            this.right.preTraversal();
        }
    }

    /**
     * @description 中序遍历
     * 先遍历左子树，再输出父节点，再遍历右子树
     * @author 俊昭
     * @date 2022/5/4
     */
    public void midTraversal() {
        if (this.left != null) {
            this.left.midTraversal();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midTraversal();
        }
    }

    /**
     * @description 后序遍历
     * 先遍历左子树，再遍历右子树，最后输出父节点
     * @author 俊昭
     * @date 2022/5/4
     */
    public void postTraversal() {
        if (this.left != null) {
            this.left.postTraversal();
        }
        if (this.right != null) {
            this.right.postTraversal();
        }
        System.out.println(this);
    }

    /**
     * @param No 查找的排名
     * @description 前序查找
     * @author 俊昭
     * @date 2022/5/4
     */
    public Node preSearch(int No) {
//        System.out.println("计数");
        if (this.No == No) {
            return this;
        }
        Node temp = null;
        if (this.left != null) {
            temp = left.preSearch(No);
        }
        if (temp != null) {
            return temp;
        }
        if (this.right != null) {
            temp = this.right.preSearch(No);
        }
        return temp;
    }

    /**
     * @param No 查找的排名
     * @description 中序查找
     * @author 俊昭
     * @date 2022/5/4
     */
    public Node midSearch(int No) {
        Node temp = null;
        if (this.left != null) {
            temp = this.left.midSearch(No);
        }

//        System.out.println("计数");
        if (this.No == No) {
            return this;
        }
        if (temp != null) {
            return temp;
        }
        if (this.right != null) {
            temp = this.right.midSearch(No);
        }
        return temp;
    }

    /**
     * @param No 查找的排名
     * @description 后序查找
     * @author 俊昭
     * @date 2022/5/4
     */
    public Node postSearch(int No) {

        Node temp = null;
        if (this.left != null) {
            temp = this.left.postSearch(No);
        }
        if (temp != null) {
            return temp;
        }
        if (this.right != null) {
            temp = this.right.postSearch(No);
        }
//        System.out.println("计数");
        if (this.No == No) {
            return this;
        }
        return temp;
    }

    /**
     * @param no 要删除的节点
     * @description 删除节点
     * @author 俊昭
     * @date 2022/5/5
     */
    public void del(int no) {

        if (this.left != null && this.left.No == no) {
            this.left = null;
        }
        if (this.right != null && this.right.No == no) {
            this.right = null;
        }
        if (this.left != null) {
            this.left.del(no);
        }
        if (this.right != null) {
            this.right.del(no);
        }


    }


    public int getNo() {
        return No;
    }

    public void setNo(int no) {
        No = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
